package com.example.lte.config;

import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.session.SaSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author liuzh
 */
@Component
public class SaTokenDaoRedis implements SaTokenDao {
    // string专用
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    // SaSession专用
    private RedisTemplate<String, SaSession> redisTemplateSa;

    @Autowired
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        JdkSerializationRedisSerializer jrSerializer = new JdkSerializationRedisSerializer();
        redisTemplate.setValueSerializer(jrSerializer);
        this.redisTemplateSa = redisTemplate;
    }

    // 根据key获取value ，如果没有，则返回空
    @Override
    public String getValue(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    // 写入指定key-value键值对，并设定过期时间(单位：秒)
    @Override
    public void setValue(String key, String value, long timeout) {
        stringRedisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }

    // 删除一个指定的key
    @Override
    public void delKey(String key) {
        stringRedisTemplate.delete(key);
    }


    // 根据指定key的session，如果没有，则返回空
    @Override
    public SaSession getSaSession(String sessionId) {
        return redisTemplateSa.opsForValue().get(sessionId);
    }

    // 将指定session持久化
    @Override
    public void saveSaSession(SaSession session, long timeout) {
        redisTemplateSa.opsForValue().set(session.getId(), session, timeout, TimeUnit.SECONDS);
    }

    // 更新指定session
    @Override
    public void updateSaSession(SaSession session) {
        long expire = redisTemplateSa.getExpire(session.getId());
        if(expire == -2) {    // -2 = 无此键
            return;
        }
        redisTemplateSa.opsForValue().set(session.getId(), session, expire, TimeUnit.SECONDS);
    }

    // 删除一个指定的session
    @Override
    public void delSaSession(String sessionId) {
        redisTemplateSa.delete(sessionId);
    }
}
